/**
 * 
 */
package com.ntv.presentation.spring.web.obituary;

import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ntv.domain.Obituary;
import com.ntv.service.DomainException;
import com.ntv.service.obituary.ObituaryAppService;

/**
 * @author Kavi
 * @Since 04/02/2009
 */
@Controller
@RequestMapping("/obituaryEntry.html")
public class ObituaryFormController {
	private static final Logger logger = Logger
			.getLogger(ObituaryFormController.class);

	@Autowired
	private ObituaryAppService obituaryAppService;
	
	@Autowired
	private CustomDateEditor customDateEditor;
	
	
	/**
	 * @return the customDateEditor
	 */
	public CustomDateEditor getCustomDateEditor() {
		return customDateEditor;
	}

	/**
	 * @param customDateEditor the customDateEditor to set
	 */
	public void setCustomDateEditor(CustomDateEditor customDateEditor) {
		this.customDateEditor = customDateEditor;
	}

	public void setObituaryAppService(ObituaryAppService obituaryAppService){
		this.obituaryAppService = obituaryAppService;
	}
	
	private static final String MA_OBITUARY = "obituary";
	

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		logger.info(" initBinder of obituary entry controller ");
		binder.registerCustomEditor(   
		        Date.class, getCustomDateEditor());   
		binder.setAllowedFields(new String[] { "age", "name", "dateOfDeath", "funeralDetails", "contactDetails", "familyMembersNames" , 
				"parentName" , "nativePlace" });
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String form(Model model) {
		model.addAttribute(MA_OBITUARY, new Obituary());
		return "obituary.form";
	}
	
	@RequestMapping(method = RequestMethod.POST)	
	public String form(@ModelAttribute(MA_OBITUARY) Obituary obituary, @RequestParam("multipartFile") MultipartFile multipartFile, ModelMap model) {
		try
		{
			obituary.setImage(multipartFile.getInputStream());
			this.obituaryAppService.saveObituary(obituary);
			logger.info("obituary msg is inserted successfully...");
			return "redirect:/obituaryhome.html";
		}
		catch (DomainException e)
		{
			logger.error(e);
			return "redirect:/obituaryhome.html";
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
			return "redirect:/wishesentry.html";
		}
	}
	
	
}
